home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / pygtk / 2.0 / codegen / scmexpr.pyo (.txt) < prev   
Python Compiled Bytecode  |  2006-01-20  |  4KB  |  167 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. from __future__ import generators
  5. import string
  6. import types
  7. from cStringIO import StringIO
  8.  
  9. class error(Exception):
  10.     
  11.     def __init__(self, filename, lineno, msg):
  12.         Exception.__init__(self, msg)
  13.         self.filename = filename
  14.         self.lineno = lineno
  15.         self.msg = msg
  16.  
  17.     
  18.     def __str__(self):
  19.         return '%s:%d: error: %s' % (self.filename, self.lineno, self.msg)
  20.  
  21.  
  22. trans = [
  23.     ' '] * 256
  24. for i in range(256):
  25.     if chr(i) in string.letters + string.digits + '_':
  26.         trans[i] = chr(i)
  27.         continue
  28.     trans[i] = '_'
  29.  
  30. trans = string.join(trans, '')
  31.  
  32. def parse(filename):
  33.     if isinstance(filename, str):
  34.         fp = open(filename, 'r')
  35.     else:
  36.         fp = filename
  37.         filename = getattr(fp, 'name', '<unknown>')
  38.     whitespace = ' \t\n\r\x0b\x0c'
  39.     nonsymbol = whitespace + '();\'"'
  40.     stack = []
  41.     openlines = []
  42.     lineno = 0
  43.     for line in fp:
  44.         pos = 0
  45.         lineno += 1
  46.         while pos < len(line):
  47.             if line[pos] in whitespace:
  48.                 pass
  49.             elif line[pos] == ';':
  50.                 break
  51.             elif line[pos:pos + 2] == "'(":
  52.                 pass
  53.             elif line[pos] == '(':
  54.                 stack.append(())
  55.                 openlines.append(lineno)
  56.             elif line[pos] == ')':
  57.                 if len(stack) == 0:
  58.                     raise error(filename, lineno, 'close parenthesis found when none open')
  59.                 
  60.                 closed = stack[-1]
  61.                 del stack[-1]
  62.                 del openlines[-1]
  63.                 if stack:
  64.                     stack[-1] += (closed,)
  65.                 else:
  66.                     yield closed
  67.             elif line[pos] == '"':
  68.                 if not stack:
  69.                     raise error(filename, lineno, 'string found outside of s-expression')
  70.                 
  71.                 endpos = pos + 1
  72.                 chars = []
  73.                 while endpos < len(line):
  74.                     if endpos + 1 < len(line) and line[endpos] == '\\':
  75.                         endpos += 1
  76.                         if line[endpos] == 'n':
  77.                             chars.append('\n')
  78.                         elif line[endpos] == 'r':
  79.                             chars.append('\r')
  80.                         elif line[endpos] == 't':
  81.                             chars.append('\t')
  82.                         else:
  83.                             chars.append('\\')
  84.                             chars.append(line[endpos])
  85.                     elif line[endpos] == '"':
  86.                         break
  87.                     else:
  88.                         chars.append(line[endpos])
  89.                     endpos += 1
  90.                 if endpos >= len(line):
  91.                     raise error(filename, lineno, 'unclosed quoted string')
  92.                 
  93.                 pos = endpos
  94.                 stack[-1] += (''.join(chars),)
  95.             elif not stack:
  96.                 raise error(filename, lineno, 'identifier found outside of s-expression')
  97.             
  98.             endpos = pos
  99.             while endpos < len(line) and line[endpos] not in nonsymbol:
  100.                 endpos += 1
  101.             symbol = line[pos:endpos]
  102.             pos = max(pos, endpos - 1)
  103.             
  104.             try:
  105.                 symbol = int(symbol)
  106.             except ValueError:
  107.                 
  108.                 try:
  109.                     symbol = float(symbol)
  110.                 except ValueError:
  111.                     pass
  112.                 except:
  113.                     None<EXCEPTION MATCH>ValueError
  114.                 
  115.  
  116.                 None<EXCEPTION MATCH>ValueError
  117.  
  118.             stack[-1] += (symbol,)
  119.             pos += 1
  120.     
  121.     if len(stack) != 0:
  122.         msg = '%d unclosed parentheses found at end of file (opened on line(s) %s)' % (len(stack), ', '.join(map(str, openlines)))
  123.         raise error(filename, lineno, msg)
  124.     
  125.  
  126.  
  127. class Parser:
  128.     
  129.     def __init__(self, filename):
  130.         '''Argument is either a string, a parse tree, or file object'''
  131.         self.filename = filename
  132.  
  133.     
  134.     def startParsing(self, filename = None):
  135.         if not filename:
  136.             pass
  137.         statements = parse(self.filename)
  138.         for statement in statements:
  139.             self.handle(statement)
  140.         
  141.  
  142.     
  143.     def handle(self, tup):
  144.         cmd = string.translate(tup[0], trans)
  145.         if hasattr(self, cmd):
  146.             getattr(self, cmd)(*tup[1:])
  147.         else:
  148.             self.unknown(tup)
  149.  
  150.     
  151.     def unknown(self, tup):
  152.         pass
  153.  
  154.  
  155. _testString = '; a scheme file\n(define-func gdk_font_load    ; a comment at end of line\n  GdkFont\n  ((string name)))\n\n(define-boxed GdkEvent\n  gdk_event_copy\n  gdk_event_free\n  "sizeof(GdkEvent)")\n'
  156. if __name__ == '__main__':
  157.     import sys
  158.     if sys.argv[1:]:
  159.         fp = open(sys.argv[1])
  160.     else:
  161.         fp = StringIO(_testString)
  162.     statements = parse(fp)
  163.     for s in statements:
  164.         print `s`
  165.     
  166.  
  167.